//
//  KWallPersonVC.m
//  Keping
//
//  Created by apple on 2017/12/12.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "KWallPersonVC.h"
#import "KWallProfilePersonCell.h"
#import "KWallProfileMessageCell.h"
#import "GPContactRequest.h"

@interface KWallPersonVC () <UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate,KWallProfileMessageCellDelegate>{
    NSArray *personTitles;
}

@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSDictionary *partyView;

@end

@implementation KWallPersonVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    personTitles = @[@"Sitting",@"Moblie",@"Alias",@"Address",@"Block"];
    [self.tableView registerNib:[UINib nibWithNibName:[KWallProfilePersonCell className] bundle:nil] forCellReuseIdentifier:[KWallProfilePersonCell className]];
    [self.tableView registerNib:[UINib nibWithNibName:[KWallProfileMessageCell className] bundle:nil] forCellReuseIdentifier:[KWallProfileMessageCell className]];
    [self getData];
}

- (void)getData {
    [super getData];
    [self showLoading];
    WEAKSELF
    NSString *url = [kGetUserInfoByPhonePath stringByReplacingOccurrencesOfString:@"{phone}" withString:self.statusLayout.status.phone];
    GPContactRequest *request = [[GPContactRequest alloc] initWithURL:url];
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf hideHUD];
        NSDictionary *dict = request.responseJSONObject;
        if ([dict[@"statusCode"] integerValue] != 200) {
            [weakSelf showErrorText:dict[@"msg"] errCode:[dict[@"statusCode"] integerValue]];
            [weakSelf.navigationController popViewControllerAnimated:YES];
            return;
        }
        id obj = dict[@"partyView"];
        if (!ValueNotNSNull(obj)) {
            [weakSelf showErrorText:@"Error accessing data!"];
            return;
        }
        weakSelf.partyView = dict[@"partyView"];
        [weakSelf.tableView reloadData];
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf hideHUD];
        [weakSelf showError:request.error];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 80.0;
    }
    return 44.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01;
    }
    return 6.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = indexPath.section;
    UITableViewCell *cell;
    switch (section) {
        case 0:{
            KWallProfilePersonCell *person = [tableView dequeueReusableCellWithIdentifier:[KWallProfilePersonCell className]];
            person.partyView = self.partyView;
            cell = person;
        }
            break;
        case 1:
        case 2:
        case 3:
        case 4:{
            KWallProfileMessageCell *message = [tableView dequeueReusableCellWithIdentifier:[KWallProfileMessageCell className]];
            message.msgTitle.text = [personTitles objectAtIndex:section];
            message.accessoryType = UITableViewCellAccessoryNone;
            message.delegate = self;
            if (section == 1){
                message.msgDetail.textColor = [UIColor blueColor];
                message.msgDetail.hidden = NO;
                message.msgSwitch.hidden = YES;
                message.msgDetail.text = [NSString stringWithFormat:@"%@ %@",[self.partyView objectForKey:@"prix"],[self.partyView objectForKey:@"phone"]];
            } else if (section == 2){
                message.msgDetail.textColor = kBlackColor;
                message.msgDetail.hidden = NO;
                message.msgSwitch.hidden = YES;
                message.msgDetail.text = [self.partyView objectForKey:@"remarks"];//别名-备注名
                message.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            } else if (section == 3) {
                message.msgDetail.textColor = kBlackColor;
                message.msgDetail.hidden = NO;
                message.msgSwitch.hidden = YES;
                message.msgDetail.text = [self.partyView objectForKey:@"communityName"];
            } else if (section == 4) {
                message.msgDetail.textColor = kBlackColor;
                message.msgDetail.hidden = YES;
                message.msgSwitch.hidden = NO;
                message.msgDetail.text = nil;
                message.msgSwitch.on = [[self.partyView objectForKey:@"isBlock"] isEqualToString:@"Y"] ? YES : NO;
            }
            cell = message;
        }
            break;
        default:
            break;
    }
    return cell;
}

- (void)wallProfileMessageForBlock:(BOOL)block {
    NSString *url = [kIMAlterUserInfoURL stringByReplacingOccurrencesOfString:@"{userId}" withString:[self.partyView objectForKey:@"friendId"]];
    GPContactRequest *request = [[GPContactRequest alloc] initWithURL:url];
    NSString *isBlock = block?@"Y":@"N";
    request.isPost = YES;
    request.params = @{@"isBlock":isBlock};
    WEAKSELF
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        NSDictionary *response = request.responseJSONObject;
        if ([response[@"statusCode"] integerValue] != 200) {
            [weakSelf showErrorText:response[@"msg"] errCode:[response[@"statusCode"] integerValue]];
        } else {
            [weakSelf showSuccess:response[@"msg"]];
        }
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf showError:request.error];
    }];
}

#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSInteger section = indexPath.section;
    if (section == 2) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                        message:@"Alias"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Save", nil];
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        [alert show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        
        UITextField *text = [alertView textFieldAtIndex:0];
        if (text.text.length == 0) {
            [self showErrorText:@"请输入正确的备注名"];
            return;
        }
        
        NSString *url = [kIMAlterUserInfoURL stringByReplacingOccurrencesOfString:@"{userId}" withString:[self.partyView objectForKey:@"id"]];
        GPContactRequest *request = [[GPContactRequest alloc] initWithURL:url];
        request.isPost = YES;
        request.params = @{@"remarks":text.text};
        WEAKSELF
        [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
            NSDictionary *response = request.responseJSONObject;
            if ([response[@"statusCode"] integerValue] != 200) {
                [weakSelf showErrorText:response[@"msg"] errCode:[response[@"statusCode"] integerValue]];
            } else {
                [weakSelf showSuccess:response[@"msg"]];
                [weakSelf performSelector:@selector(getData) withObject:nil afterDelay:1.5];
            }
        } failure:^(__kindof KBaseRequest * _Nonnull request) {
            [weakSelf showError:request.error];
        }];
    }
}

@end
